/*-----------------------------------------------------
 * Html编辑器与Text编辑器切换组件
 * ---------------------------------------------------*/
jx.switchHtmlTextEditor = function () {
    //私有变量
    var $tabs, $tabContent, $content, htmlOps, textOps;
    var htmlId = 'html_ueditor_content_id', textId = 'md_text_content_id';
    var htmlInstance, txtInstance;
    var currentContent, currentFormat;

    var clearHtml =function (htmlStr){
        return htmlStr.replace(/<\/?.+?>/g, "").replace(/ /g, "");
    };
    //私有方法
    var ueditorInit = function () {
        var $editor = $('<textarea id="' + htmlId + '"></textarea>');
        $editor.val(currentContent);
        $tabContent.append($editor);
        htmlInstance = UE.getEditor(htmlId, $.extend({
            enableAutoSave: true,   //启用自动保存
            saveInterval: 2000      //自动保存间隔时间， 单位ms
        }, htmlOps));
    };
    var ueditorGetText = function () {
        if (htmlInstance) {
            return clearHtml(UE.getEditor(htmlId).getPlainTxt());
        }
        return clearHtml($content.val());
    };
    var ueditorDestroy = function () {
        if (htmlInstance) {
            UE.getEditor(htmlId).destroy();
            $tabContent.empty();
            htmlInstance = null;
        }
    };

    var textInit = function () {
        var width = textOps.width || '100%';
        var height = textOps.height || '300px';
        var cls = textOps.cls || 'form-control';
        txtInstance = $('<textarea id="' + textId + '" class="' + cls + '" style="width: ' + width + ';height: ' + height + ';resize: none;"></textarea>');
        txtInstance.val(currentContent);
        $tabContent.append(txtInstance);
    };
    var textGetHtml = function () {
        if (txtInstance) {
            return txtInstance.val();
        }
        return $content.val();
    };
    var textDestroy = function () {
        if (txtInstance) {
            $tabContent.empty();
            txtInstance = null;
        }
    };

    var switchEditor = function (format) {
        if (format == '1') { //html
            currentContent = textGetHtml();
            textDestroy();
            ueditorInit();
        }
        else if (format == '2') { //text
            currentContent = ueditorGetText();
            ueditorDestroy();
            textInit();
        }
        var $item = $tabs.find('a[data-format="' + format + '"]');
        $item.closest('ul').find('li').removeClass('active');
        $item.parent().addClass('active');
        currentFormat = format;
    };
    var bingClick = function () {
        $tabs.find('ul>li>a').click(function () {
            var currentFormat = $tabs.find('ul>li.active>a').data('format');
            var $this = $(this);
            var format = $this.data('format');
            if (currentFormat == format) {
                return;
            }
            switchEditor(format);
        });
    };

    //公共方法
    return {
        init: function (_$tabs, _$content, _format, _htmlOps, _textOps) {
            $tabs = _$tabs;
            $tabContent = _$tabs.find('.tab-content>div');
            $content = _$content;
            htmlOps = _htmlOps;
            textOps = _textOps;
            bingClick();
            switchEditor(_format);
        },
        getContent: function () {
            if (currentFormat == '1') { //html
                return UE.getEditor(htmlId).getContent();
            }
            else if (currentFormat == '2') { //text
                return txtInstance.val();
            }
        },
        getFormat: function () {
            return currentFormat;
        }
    };
}();